package com.qf.commons.core.utils;

import com.qf.commons.core.exception.ServiceException;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author YaoJiXing
 * created 2022/9/9 21:54
 */
public class QfBeanUtils {

    public static <T> T copyBean(Object source, Class<T> target) {
        try {
            T t = target.newInstance();
            BeanUtils.copyProperties(source, t);
            return t;
        } catch (Exception e) {
            throw new ServiceException(500, target.getName() + "初始化异常");
        }
    }

    public static <T> List<T> copyList(Collection collection, Class<T> tcls){
        if (collection == null || collection.size() == 0) return null;

        List<T> targetList = new ArrayList<>();
        //循环集合
        for (Object sourceObj : collection) {
            //逐个拷贝
            T targetObj = copyBean(sourceObj, tcls);
            targetList.add(targetObj);
        }
        return targetList;
    }

}
